package org.ccu.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccu.eshop.domain.dto.CategoryVO;
import org.ccu.eshop.service.CategoryServiceItface;
import org.ccu.eshop.service.impl.CategoryServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
* s商品类别控制器 增删改查
* */
@WebServlet("/api/category")
public class CategoryController extends HttpServlet {

    CategoryServiceItface categoryService=new CategoryServiceimpl();//多态


    String method="";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       method=request.getParameter("m");
       if("list".equals(method)){
        list(request,response);
       }else if("del".equals(method)){
        delete(request,response);
       }
    }

     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         if("add".equals(method)){
             add(request,response);
         }else if("update".equals(method)){
             update(request,response);
         }
     }


 //添加商品数据
 private void add(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
 }

//删除商品数据
 private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
 }
 //更新商品数据
 private void update(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
 }
 //查询商品列表
 private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    /* List<CategoryVO> categoryList=new ArrayList<>();
     CategoryVO vo=new CategoryVO();
     vo.setId(1);
     vo.setCateName("计算机类");
     vo.setCateDesc("计算机类的书籍");
     CategoryVO vo2=new CategoryVO();
     vo2.setId(2);
     vo2.setCateName("音乐类");
     vo2.setCateDesc("音乐类的书籍");
     CategoryVO vo3=new CategoryVO();
     vo3.setId(3);
     vo3.setCateName("美术类");
     vo3.setCateDesc("美术类的书籍");
        categoryList.add(vo);
        categoryList.add(vo2);
        categoryList.add(vo3);*/

     List<CategoryVO> categoryList=categoryService.getCategoryList();
        //利用json转变为字符串传回前端
     String data= JSON.toJSONString(categoryList);
     response.getWriter().write(data);
 }

}
